/**
 * 
 */
package com.zte.claa.inficombo.csa.app.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期辅助类
 * @author 0283000138
 */
public class DateUtils {
	private static final String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss SSS";
	
	private static final String DATE_FORMAT = "yyyy-MM-dd";
	
	private static final int[] TIME_FIRST = new int[]{0, 0, 0, 0};
	
	private static final int[] TIME_LAST = new int[]{23, 59, 59, 000};
	//将Date类型转为Calendar类型
	public static Calendar calendar(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar;
	}
	//获取指定日期的年份
	public static int getYear(Date date){
		return calendar(date).get(Calendar.YEAR);
	}
	
	//获取指定日期的年份
	public static int getMonth(Date date){
		return calendar(date).get(Calendar.MONTH);
	}
	
	//获取今年的年份
	public static int getThisYear(Date current){
		return getYear(current);
	}
	
	//获取去年的年份
	public static int getLastYear(Date current){
		return getThisYear(current) - 1;
	}
	
	//返回去年的第一天
	public static Date getFirstDaytimeLastYear(Date current){
		return getFirstDayInYear(getLastYear(current));
	}
	
	//返回今年的第一天
	public static Date getFirstDaytimeThisYear(Date current){
		return getFirstDayInYear(getThisYear(current));
	}
	
	//返回上个月的第一天
	public static Date getFirstDaytimeLastMonth(Date current){
		Calendar calendar = calendar(current);//获取当前日期 
		calendar.add(Calendar.MONTH, -1);
		calendar.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天 
		
		return getFirstDatetime(calendar);
	}
	
	//返回这个月的第一天
	public static Date getFirstDaytimeThisMonth(Date current){
		Calendar calendar = calendar(current);//获取当前日期 
		calendar.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天 
		
		return getFirstDatetime(calendar);
	}	
	
	//获取开始时间
	public static Date getFirstDatetime(Calendar calendar){
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		return getDatetime(year, month, day, TIME_FIRST);
	}
	
	//获取结束时间
	public static Date getLastDatetime(Calendar calendar){
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		return getDatetime(year, month, day, TIME_LAST);
	}
	
	//返回去年的最后一天
	public static Date getLastDaytimeLastYear(Date current){
		return getLastDayInYear(getLastYear(current));
	}
	
	//返回上个月的最后一天
	public static Date getLastDaytimeLastMonth(Date current){
		return getLastDayInYear(getLastYear(current));
	}
	//返回昨天
	public static Date getLastDaytime(Date date) {
		Calendar calendar = calendar(date);
		calendar.add(Calendar.DAY_OF_MONTH, -1); //日期减1
		
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		
		return getDatetime(year, month, day, TIME_LAST);
	}
	
	//相同的日期
	public static boolean sameDay(Date d1, Date d2){
		Calendar cal1 = Calendar.getInstance();  
	    cal1.setTime(d1);  
	    cal1.set(Calendar.HOUR_OF_DAY, 0);  
	    cal1.set(Calendar.MINUTE, 0);  
	    cal1.set(Calendar.SECOND, 0);  
	    cal1.set(Calendar.MILLISECOND, 0); 
	    
	    Calendar cal2 = Calendar.getInstance(); 
	    cal2.setTime(d2);  
	    cal2.set(Calendar.HOUR_OF_DAY, 0);  
	    cal2.set(Calendar.MINUTE, 0);  
	    cal2.set(Calendar.SECOND, 0);  
	    cal2.set(Calendar.MILLISECOND, 0); 
	    
	    return  cal1.getTime().equals(cal2.getTime()); 
	}
	
	//获取某年的第一天, 时间为0:0:0
	public static Date getFirstDayInYear(int year){
		return getDatetime(year, 0, 1, TIME_FIRST);
	}
	
	//获取某年的最后一天, 时间为23:59:59 999
	public static Date getLastDayInYear(int year){
		return getDatetime(year, 11, 31, TIME_LAST);
	}
	
	public static Date getDatetime(int year, int month, int day, int[] times){
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DATE, day);
		calendar.set(Calendar.MONTH, month);
		calendar.set(Calendar.YEAR, year);
		
		calendar.set(Calendar.HOUR_OF_DAY, times[0]);  
		calendar.set(Calendar.MINUTE, times[1]);  
		calendar.set(Calendar.SECOND, times[2]);  
		calendar.set(Calendar.MILLISECOND, times[3]);
		
		return calendar.getTime();
	}
	
	public static Date getDatetime(int year, int month, int day){
		return getDatetime(year,month,day, TIME_FIRST);
	}
	
	public static Date getDatetime(Date date, int[] times){
		Calendar calendar = calendar(date);
		
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		return getDatetime(year,month,day, times);
		
	}
	
	public static String getDateString(int year, int month, int day){
		Date date = getDatetime(year, month, day, TIME_FIRST);
		return format(date);
	}
	
	public static Date getToday(){
		return new Date();
	}
	
	public static Date parse(String date) throws ParseException{
		return new SimpleDateFormat(DATE_FORMAT).parse(date);
	}
	
	public static String format(Date date){
		return format(date, new SimpleDateFormat(TIME_FORMAT));
	}
	
	public static String format(Date date, SimpleDateFormat sdf){
		return sdf.format(date.getTime());
	}
	
	//将Date格式转成Timestamp格式
	public static Timestamp toTimestamp(Date date){
		return new Timestamp(date.getTime());
	}
	
	//返回date1和date2中，日期较新的那个
	public static Date getNewDate(Date date1, Date date2){
		return date1.after(date2)? date1:date2;
	}
	
	//返回date1和date2中，日期较老的那个
	public static Date getOldDate(Date date1, Date date2){
		return date1.before(date2)? date1:date2;
	}
	
	//计算日期之间有几个月
	public static int monthBetween(Date start, Date end){
		Calendar cal1 = calendar(start);
		Calendar cal2 = calendar(end);
		
		return (cal2.get(Calendar.YEAR) - cal1.get(Calendar.YEAR)) * 12 + 
				cal2.get(Calendar.MONTH)- cal1.get(Calendar.MONTH) + 1;
	}
	
	//获取前几天
	public static Date getBeforeDate(Date date, int before){
		Calendar cal1 = calendar(date);
		cal1.add(Calendar.DATE, -before);
		return cal1.getTime();
	}
	
	//获取日期开始时间
	public static Date getDayStart(Date date){
		return getDatetime(date, TIME_FIRST);
	}
	
	//获取日期结束时间
	public static Date getDayEnd(Date date){
		return getDatetime(date, TIME_LAST);
	}
	
}
